/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.facade;

import aviso.entity.Area;
import aviso.entity.AreaType;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author avgusti
 */
@Stateless
public class DataGenFacade {

    @PersistenceContext(unitName = "aviso")
    private EntityManager em;

    
    protected EntityManager getEntityManager() {
        return em;
    }
    public void initAreas()
    {
        AreaType c = new AreaType();
        c.setName("Country");
        c.setId(1l);
        em.persist(c);
        AreaType ct = new AreaType();
        ct.setName("City");
        ct.setId(2l);
        em.persist(ct);
        AreaType st = new AreaType();
        st.setName("Street");
        st.setId(3l);
        em.persist(st);

        Area contry = new Area();
        contry.setName("Украина");
        em.persist(contry);
        contry.setType(c);
        em.merge(contry);

        Area city = new Area();
        city.setName("Киев");
        em.persist(city);
        city.setType(ct);
        city.setParent(contry);
        contry.getChildren().add(city);
        em.merge(city);
        em.merge(contry);

        List<String> o=(List<String>)em.createNativeQuery("SELECT Name FROM STREET").getResultList();
        for(String name:o)
        {
            Area a=new Area();
            a.setName(name);
            em.persist(a);
            a.setParent(city);
            city.getChildren().add(a);
            a.setType(st);
            em.merge(a);
            em.merge(city);
        }
        
    
    }
    
    public void deleteAreas()
    {
        em.createNativeQuery("delete from Geotag_geotag").executeUpdate();
        em.createNativeQuery("delete from Geotag").executeUpdate();
        em.createNativeQuery("delete from AreaType").executeUpdate();
    }
    public void deleteAdvs()
    {
        em.createNativeQuery("delete from Advert").executeUpdate();
    }
    
}
